package com.cindata

import grails.transaction.Transactional

@Transactional
class RiskWarningFlowStageService
{
    def initFlowStage(RiskWarning riskWarning, Workflow workflow)
    {
        def executionSequence = RiskWarningFlowStage.findByRiskWarning(riskWarning, [sort: 'executionSequence', order: 'desc'])?.executionSequence
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        workflowStages?.each {
            def riskWarningFlowStage = new RiskWarningFlowStage()
            riskWarningFlowStage.executionSequence = executionSequence ? (executionSequence + it?.executionSequence) : it?.executionSequence
            riskWarningFlowStage.stage = it?.stage
            riskWarningFlowStage.canReject = it?.canReject
            riskWarningFlowStage.layout = it?.layout
            riskWarningFlowStage.riskWarning = riskWarning
            if (riskWarningFlowStage.validate())
            {
                riskWarningFlowStage.save flush: true
            }
            else
            {
                println riskWarningFlowStage.errors
            }
        }
    }
}
